package sctu.edu.java.year2016.team08.lisonghong.day20160616.chapter15_2;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by 松红 on 2016/6/16.
 * 尝试开发一个窗体，在窗体中有两个按钮，一个是“开始”按钮，一个是“结束”按钮，当用
 * 用户点击“开始”按钮时，在控制台中持续打印一段话，当用户单击“停止”按钮时，控制台结束打印。
 */
public class StartAndEnd extends JFrame {

    Thread thread;

    public StartAndEnd() {
        JPanel contentPane = new JPanel();
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);
        JButton button1 = new JButton("start");
        contentPane.add(button1, BorderLayout.NORTH);
        JButton button2 = new JButton("end");
        contentPane.add(button2, BorderLayout.SOUTH);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(100,100);
        setVisible(true);
        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
//                for (int i = 1; i > 0; i++) {
//                    System.out.println("start!!!!!!!!!!!");
//                }
//                int i=1;
//                while(i>0){
//                    System.out.println("start!!!!!!!!!!!");
//                }
//                for 和while循环都不能使之停止！
                thread = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        for (int i = 1; i > 0; i++) {
                            System.out.println("start!!!!!!!!!!");
                        }

                    }
                });
                thread.start();
            }
        });
        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });

    }

    public static void main(String[] args) {
        new StartAndEnd();
    }

}
